﻿using Windows.Storage;
using System;
using Windows.Storage.AccessCache;
using System.Collections.Generic;

namespace SharPy.GameSettings
{
    internal static class clSettings
    {
        public static event EventHandler<SettingChanged> ChangeSetting;

        private static ApplicationDataContainer localSettings;

        public static Windows.UI.Color defaultTextColor = Windows.UI.Colors.White;

        public static Dictionary<string, int> GameSettings = new Dictionary<string,int>(); 

        ///Saves settings value to local settings
        ///
        public static void saveSetting(string pSetting, int pValue)
        {
            localSettings.Values[pSetting] = pValue;
        }

        ///Saves and gets path to last game list
        ///
        public static void saveGameList(StorageFile pFile)
        {
            if (pFile != null)
                localSettings.Values["gameListPath"] = pFile.Path;
        }
        public static string getGameList()
        {
            return localSettings.Values["gameListPath"] as string;
        }

        /// <summary>
        /// Procedures to save and get last game folder
        /// </summary>
        /// <param name="pGameFolder"></param>
        public static void saveFolder(StorageFolder pGameFolder)
        {
            localSettings.Values["lastGameFolder"] = pGameFolder.Path;
        }
        public async static System.Threading.Tasks.Task<StorageFolder> getFolder()
        {
            string a = localSettings.Values["lastGameFolder"].ToString();
            var b = await StorageFolder.GetFolderFromPathAsync(a); 
            return b; 
        }

        ///Static constructor to initialize local settings
        ///
        static clSettings()
        {
            localSettings = ApplicationData.Current.LocalSettings;

            GameSettings.Add("FontSize", 32);
        }

        /// <summary>
        /// Game saving
        /// </summary>
        /// <param name="plineNoHolder"></param>
        internal static void saveGameState(int plineNoHolder)
        {
            localSettings.Values["lastGameState"] = plineNoHolder; 
        }

        internal static int dropGameState()
        {
            localSettings.Values["lastGameState"] = 0;
            return 0;
        }

        internal static int getGameState()
        {
            return Convert.ToInt32(localSettings.Values["lastGameState"]);
        }

        internal static void SetSetting(string key, int value)
        {
            if (GameSettings.ContainsKey(key))
            {
                GameSettings[key] = value;
            }
            else
            {
                GameSettings.Add(key, value);
            }
            OnChangeSetting(new SettingChanged(key, value));
        }

        internal static int GetSetting(string key)
        {
            if (GameSettings.ContainsKey(key))
                return GameSettings[key];
            else 
                throw new ArgumentException("Incorrect setting name.");
        }

        public static void OnChangeSetting(SettingChanged pSetting)
        {
            EventHandler<SettingChanged> handler = ChangeSetting;

            if (handler != null)
            {
                handler(null, pSetting);
            }
        }
    }

    public class SettingChanged : EventArgs
    {
        public string Key;
        public int Value;

        public SettingChanged() { }

        public SettingChanged(string key, int val)
        {
            this.Key = key;
            this.Value = val;
        }
    }
}
